<?php

namespace Cyfy\Modules\TinyMCE;

class TinyMCE extends \Cyfy\Translatable
{
    static $hasRun = false;

    static function get($name = "content", $code = "content", $value = ""){
        $uniq = "tinymce_" . uniqid();
        $output .= "<div class=\"tinymce\">\n";
        $output .= "<p class=\"header\">{$name}:</p>\n";
        $output .= "  <textarea id=\"{$uniq}\" name=\"{$code}\" style=\"width:100%;\">{$value}</textarea>\n";
        $output .= \Cyfy\Cyfy ::  javaScript("tinyMCE.execCommand('mceAddControl',false,'{$uniq}');", "inline");
        $output .= "</div>\n";
        self :: runonce();
        return $output;
    }
  
    static function runonce()
    {
        if(self :: $hasRun){
            return false;
        }
        self :: $hasRun = true;
        \Cyfy\Cyfy ::  css(\Cyfy\Cyfy :: getSourcePath("Cyfy/Modules/TinyMCE/wysiwyg.css"), "file");
        \Cyfy\Cyfy ::  javascript(\Cyfy\Cyfy :: getSourcePath("Cyfy/Modules/TinyMCE/tinymce/jscripts/tiny_mce/tiny_mce.js"), "file");
        $lg = \Cyfy\Language :: getActive();        
        $turl = \Cyfy\Template :: $url;
        $noBorder = self :: t(10);
        \Cyfy\Cyfy ::  javaScript(<<<JAVASCRIPT
tinyMCE.init({
    mode : "none",
    relative_urls : false,
    language : "{$lg}",
	elements : "{$uniq}",
    theme : "advanced",
    entity_encoding : "raw",
	extended_valid_elements : "iframe[src|width|height|name|align|scrolling|frameborder]",
	plugins : "autoresize,safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,code,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,media,advhr,|,print,|,fullscreen",
	theme_advanced_toolbar_location : "bottom",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	theme_advanced_resizing : true,
    theme_advanced_blockformats : "p,h2,h3,h4,h5,h6",
	content_css : "{$turl}wysiwyg.css",
    file_browser_callback : "fmTinyMCEWindow",
});
JAVASCRIPT
, "inline", "header");
    
        if(\Cyfy\Modules :: isEnabled("FileManager")){
            \Cyfy\Modules\jquery\Plugins :: load("FileManager");
           
        }
    }
}
